package app;

import app.cryp.AESCipher;
import app.cryp.AndroidBase64;
import app.cryp.RSAUtils;
import org.apache.commons.lang.StringUtils;

import java.net.URLDecoder;
import java.util.StringTokenizer;

public class AppDecry {
    public static void main(String[] args) {
      String ss= decry("8jxqaXUIiQcRzuFrHYmIY8Fz4bywtv4nm8pVNeNCCDNw1DyL+AKD4kt/E/Czr0hEXmwqOQi/Kfhjryh+1Y7JFHMQQ3vcJD6c5Ope9WfUt0Pug601la64A7GU2eYG4GvFXUg/d5hf/1rJLIavAOA8/Zgfauax28PliNl10NArCdDBtBfgZs+5SnAYU6PZP2++HJaMNNfNTZti4S8HQ+0hkMxHhkeaaZmbXV456g9wtUrTgky99MZzaDEmLwCLEQ/6uTNBgwPMAC1n19xHIp2xqI02GA5198pcTjA+BLrykNMH4iELuau7/NLLFUwGWrfkznS/aMkJLYWBXWMH9GNgVeNuivsgRx3wwMJW98WrL6PwHGF9JT/ARujGCbbJzKNXC5MFe16t1M6jmZ2yfQCFY4/EIX/9Dh9uC1BCDUmvP1gQTJSUtOBZMFg1nc5xECXNG/sQ3Ce/urjSpsRSRh6CBI+avuyXQicpd2hJk67jB593AskSFuy0C7ZWbzSs+tGU40HgH2iQZO1hNpKdhg94b5K04aAyYX0XrASXLC+o8cI=");
        System.out.println(ss);
    }

    public static String decry(String tranferStr){
        String content = "";
        String[] arr = new String[3];
        try {
            // 设置编码格式为UTF-8
            // req.setCharacterEncoding("UTF-8");
            if (StringUtils.isNotEmpty(tranferStr)) {
                StringTokenizer token = new StringTokenizer(tranferStr, "♂");
                String rsaedBaseedCode = null;
                String aesedBaseedContent = null;
                if (token.hasMoreElements()) {
                    rsaedBaseedCode = token.nextToken();
                    if (StringUtils.isNotEmpty(rsaedBaseedCode)) {
                        if (token.hasMoreElements()) {
                            aesedBaseedContent = token.nextToken();
                            if (StringUtils.isNotEmpty(aesedBaseedContent)) {
                                String aesPassword = RSAUtils.decryptByPrivateKey(AndroidBase64.decode(
                                        (rsaedBaseedCode), AndroidBase64.NO_WRAP));
                                // 客户端传来的明文
                                content = new String(AESCipher.decrypt(
                                        AndroidBase64.decode(aesedBaseedContent, AndroidBase64.NO_WRAP), aesPassword),"utf-8");
                                if (!StringUtils.isEmpty(content)) {
                                    // 用前半部分解密得到的密钥 用AES再解密 content 即为最终的解密字符串
                                    arr[0] = URLDecoder.decode(content, "UTF-8");
                                    arr[1] = aesPassword;
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content;
    }
}
